package cn.scantd.core.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.TagElement;
import org.eclipse.jdt.core.dom.TextElement;

import cn.scantd.kit.StrKit;

public class JavaDocTag implements Serializable {

    private static final long serialVersionUID = 2875657609971008280L;
    
    private String tagName;
    private String refName;
    private List<String> descriptions = new ArrayList<String>();
    
    @SuppressWarnings("unchecked")
    public JavaDocTag(TagElement tag) {
        tagName = StrKit.trim(tag.getTagName());
        List<Object> fragments = tag.fragments();
        if (fragments != null && fragments.size() > 0) {
            Object first = fragments.get(0);
            if (first instanceof TextElement) {
                for(int i = 0; i < fragments.size(); i++) {
                    Object fragment = fragments.get(i);
                    if (fragment instanceof TextElement) {
                        descriptions.add(StrKit.trim(((TextElement) fragment).getText()));
                    } else if (fragment instanceof SimpleName) {
                        // 类似<M>这种形式的参数，忽略为TextElement形式
                        descriptions.add(StrKit.trim(((SimpleName) fragment).getFullyQualifiedName()));
                    }
                }
            } else if (first instanceof SimpleName) {
                refName = StrKit.trim(((SimpleName) first).getFullyQualifiedName());
                for(int i = 1; i < fragments.size(); i++) {
                    Object fragment = fragments.get(i);
                    if (fragment instanceof TextElement) {
                        descriptions.add(StrKit.trim(((TextElement) fragment).getText()));
                    } else if (fragment instanceof SimpleName) {
                        descriptions.add(StrKit.trim(((SimpleName) fragment).getFullyQualifiedName()));
                    }
                }
            }
        }
    }
    
    public String getTagName() {
        return tagName;
    }
    public void setTagName(String tagName) {
        this.tagName = tagName;
    }
    public String getRefName() {
        return refName;
    }
    public void setRefName(String refName) {
        this.refName = refName;
    }
    public List<String> getDescriptions() {
        return descriptions;
    }
    public void setDescriptions(List<String> descriptions) {
        this.descriptions = descriptions;
    }
    
}
